package com.shou.infrastructure.persistent.repository.reservation;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.reserve.model.ReservationEntity;
import com.shou.domain.reserve.repository.IReservationRepository;
import com.shou.infrastructure.persistent.dao.IReservationDao;
import com.shou.infrastructure.persistent.po.ReservationPO;
import com.shou.infrastructure.persistent.repository.reservation.mapstruct.ReservationPOConvert;
import jakarta.annotation.Resource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
 * ClassName:ReservationRepository
 * Project:SmartPark
 * Package: com.shou.infrastructure.persistent.repository.reservation
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/7 23:51
 * @Version 1.0
 */
@Repository("reservationRepository")
public class ReservationRepository implements IReservationRepository {
    @Resource
    private IReservationDao reservationDao;
    @Resource
    private ReservationPOConvert reservationPOConvert;
    @Override
    public Integer insert(ReservationEntity reservation) {
        ReservationPO reservationPO = reservationPOConvert.convertReservationEntityToPO(reservation);
        try {
            reservationDao.insertSelective(reservationPO);
            return reservationPO.getId();
        }catch (DataIntegrityViolationException e) {
            Throwable rootCause = e.getRootCause();
            if (rootCause instanceof SQLException && "22001".equals(((SQLException) rootCause).getSQLState())) {
                throw new IllegalArgumentException("输入数据过长导致SQL执行失败");
            } else {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
    }

    @Override
    public void updateReservationById(ReservationEntity reservation) {
        ReservationPO reservationPO = reservationPOConvert.convertReservationEntityToPO(reservation);
        reservationDao.updateByPrimaryKeySelective(reservationPO);
    }

    @Override
    public ReservationEntity getReservationById(Integer id) {
        ReservationPO reservationPO = reservationDao.selectById(id);
        return reservationPOConvert.convertReservationPOToEntity(reservationPO);
    }

    @Override
    public IPage<ReservationEntity> getReservationsPage(PageQueryEntity pageQueryEntity) {
        // 根据页码和页面大小,构建分页对象
        Page<ReservationPO> page = new Page<>(pageQueryEntity.getPageIndex(), pageQueryEntity.getPageSize());
        // 采用模糊查询
        IPage<ReservationPO> pageMessage = reservationDao.selectPageMessageByCondition(
                page,
                pageQueryEntity.getUserId(),
                pageQueryEntity.getParkingSpaceCode(),
                pageQueryEntity.getStatus(),
                pageQueryEntity.getRemark(),
                pageQueryEntity.getSortName(),
                pageQueryEntity.getSort()
        );
        return reservationPOConvert.convertReservationPOPageToEntity(pageMessage);
    }
}
